package pl.enigmatic.math.interpolation;

public class CubicHermiteSplineInterpolator extends Interpolator.Single {

	double tangents[];

	public CubicHermiteSplineInterpolator(final double[] frames, final double[] values, final double[] tangents) {
		super(frames, values);
		this.tangents = tangents;
	}

	@Override
	protected double getValue(final double alpha, final int rng, final double t) {
		return h00(t) * value(rng) + h10(t) * tangents[rng] * (frame(rng + 1) - frame(rng)) + h01(t) * value(rng + 1) + h11(t) * tangents[rng + 1]
				* (frame(rng + 1) - frame(rng));
	}

	double h00(final double t) {
		return (1 + 2 * t) * (1 - t) * (1 - t);
	}

	double h10(final double t) {
		return t * (1 - t) * (1 - t);
	}

	double h01(final double t) {
		return t * t * (3 - 2 * t);
	}

	double h11(final double t) {
		return t * t * (t - 1);
	}
}
